//
//  OTHomeViewController.m
//  OT_OC
//
//  Created by yunmian on 2019/7/15.
//  Copyright © 2019 James. All rights reserved.
//

#import "OTHomeViewController.h"
#import "OTAddGroupViewController.h"

#import "OTEmployeeCell.h"
#import "OTGroupView.h"

#import "OTGroupModel.h"
#import "OTEmployeeModel.h"
#import "OTMainModel.h"

@interface OTHomeViewController ()<UITableViewDelegate, UITableViewDataSource, OTAddGroupViewControllerDelegate>
{
    UIButton                *addButton;
    UITableView             *tableView;
    NSMutableArray          *datasource;
}
@end

@implementation OTHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController setNavigationBarHidden:YES];
    
    [self initUI];
    [self initData];
}

- (void)initUI {
    
    OTNavBar *navBar = [OTNavBar createNavBar];
    navBar.title = @"首页";
    [self.view addSubview:navBar];
    
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, k_Height_NavBar, kScreenWidth, self.view.height - k_Height_NavBar)];
    tableView.backgroundColor = UIColor.whiteColor;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.tableFooterView = [UIView new];
    [tableView registerClass:[OTEmployeeCell class] forCellReuseIdentifier:@"OTEmployeeCell"];
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(k_Height_NavBar, 0, 0, 0));
    }];
    
    addButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
    
    [addButton addTarget:self action:@selector(onClickAdd) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addButton];
    
    [addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-100);
        make.centerX.equalTo(self.view);
        make.size.offset(50);
    }];
}

- (void)initData {
    NSMutableArray *tempArray = [NSMutableArray array];
//    if ([OTMainModel bg_findAll:ot_tablename]) {
//        NSArray *arr =[OTMainModel bg_findAll:ot_tablename];
//
//        NSLog(@"1");
//    }
    
//    NSString *where = [NSString stringWithFormat:@"where %@='%@'",bg_sqlKey(@"groupName"),@"润工作"];
//    if ([OTMainModel bg_find:ot_tablename where:where]) {
//        NSArray *arr = [OTMainModel bg_find:ot_tablename where:where];
//        NSLog(@"1");
//    }
    
    
    
    
//    NSArray *tempArr = [OTGroupModel bg_findAll:ot_tablename];
    
    datasource = [NSMutableArray array];
    
//    [datasource setArray:tempArr];
    
//    [NSObject bg_find:ot_tablename where:<#(NSString * _Nullable)#>]
    
    
    
    [tableView reloadData];
}

#pragma mark Event
- (void)onClickAdd {
    OTAddGroupViewController *vc = [[OTAddGroupViewController alloc] init];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return datasource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    OTGroupModel *group = datasource[section];
    return group.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OTEmployeeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OTEmployeeCell" forIndexPath:indexPath];
    
    OTGroupModel *group = datasource[indexPath.section];
    OTEmployeeModel *employee = group.list[indexPath.row];
    cell.employee = employee;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    OTGroupModel *group = datasource[section];
    
    OTGroupView *view = [[OTGroupView alloc] init];
    view.title = group.groupName;
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    OTGroupModel *group = datasource[indexPath.section];
    OTEmployeeModel *employee = group.list[indexPath.row];
    
    NSDictionary *params = @{
                             @"groupName" : group.groupName,
                             @"employee" : employee
                             };
    
    OTAddGroupViewController *vc = [[OTAddGroupViewController alloc] initWithParams:params];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

#pragma mark OTAddGroupViewControllerDelegate
- (void)addGroupFinish {
    [self initData];
}

@end
